/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.libspml.impl;

import java.util.List;

import com.googlecode.libspml.AbstractSemanticSyntaxNode;
import com.googlecode.libspml.AbstractToken;
import com.googlecode.libspml.ISemanticSyntaxTreeProvider;
import com.googlecode.libspml.ITokenProvider.Type;
import com.googlecode.libspml.InvalidSyntaxException;

/**
 * @author Roger Cockshutt
 *
 */
public class SPMLSemanticSyntaxTreeProvider implements
		ISemanticSyntaxTreeProvider {

	/**
	 * 
	 */
	public SPMLSemanticSyntaxTreeProvider() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.googlecode.libspml.ISemanticSyntaxTreeProvider#buildTree(java.util.List)
	 */
	@Override
	public SPMLSemanticSyntaxTree buildTree(List<AbstractToken> tokens) {
		try {
			return new SPMLSemanticSyntaxTree(build(null, tokens));
		} catch (InvalidSyntaxException exception) {
			// TODO Auto-generated catch block
			exception.printStackTrace();
		}
		return null;
	}
	
	
	
	private static AbstractSemanticSyntaxNode build(AbstractSemanticSyntaxNode parent, List<AbstractToken> tokens) throws InvalidSyntaxException {
		AbstractSemanticSyntaxNode newNode = null;
		if (parent == null) {
			// We are building the root node.
			newNode = SemanticSymbol.MODEL.makeNode(null);
			return build(newNode, tokens);
		}
		
		AbstractToken currentToken = tokens.remove(0);
		switch (currentToken.getType()) {
		case DECLARATOR_OBJECT:
			if (tokens.get(0).getType() == Type.IDENTIFIER) {
				newNode = SemanticSymbol.OBJECT_DEFINITION.makeNode(null);
				newNode.addSubNode(SemanticSymbol.DECLARATOR.makeNode(currentToken.getLiteral()));
				newNode.addSubNode(SemanticSymbol.IDENTIFIER.makeNode(tokens.remove(0).getLiteral()));
				return build(newNode, tokens);
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.MISSING_IDENT + currentToken.getLiteral());
		case DECLARATOR_PROPERTY:
			if (tokens.get(0).getType() == Type.IDENTIFIER) {
				newNode = SemanticSymbol.PROPERTY_DEFINITION.makeNode(null);
				newNode.addSubNode(SemanticSymbol.DECLARATOR.makeNode(currentToken.getLiteral()));
				newNode.addSubNode(SemanticSymbol.IDENTIFIER.makeNode(tokens.remove(0).getLiteral()));
				return build(newNode, tokens);
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.MISSING_IDENT + currentToken.getLiteral());
		case DECLARATOR_SCOPE:
			if (tokens.get(0).getType() == Type.IDENTIFIER) {
				newNode = SemanticSymbol.SCOPE_DEFINITION.makeNode(null);
				newNode.addSubNode(SemanticSymbol.DECLARATOR.makeNode(currentToken.getLiteral()));
				newNode.addSubNode(SemanticSymbol.IDENTIFIER.makeNode(tokens.remove(0).getLiteral()));
				return build(newNode, tokens);
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.MISSING_IDENT + currentToken.getLiteral());
		case DECLARATOR_VALUE:
			if (tokens.get(0).getType() == Type.IDENTIFIER) {
				newNode = SemanticSymbol.VALUE_DECLARATION.makeNode(null);
				newNode.addSubNode(SemanticSymbol.DECLARATOR.makeNode(currentToken.getLiteral()));
				newNode.addSubNode(SemanticSymbol.IDENTIFIER.makeNode(tokens.remove(0).getLiteral()));
				return newNode;
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.MISSING_IDENT + currentToken.getLiteral());
		case IDENTIFIER:
			newNode = SemanticSymbol.IDENTIFIER.makeNode(currentToken.getLiteral());
			return newNode;
		case TERMINATOR_OBJECT:
			if (parent.getSymbol() == SemanticSymbol.OBJECT_DEFINITION) {
				newNode = SemanticSymbol.TERMINATOR.makeNode(currentToken.getLiteral());
				return newNode;
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.UNEXP_TERM + currentToken.getLiteral());
		case TERMINATOR_PROPERTY:
			if (parent.getSymbol() == SemanticSymbol.PROPERTY_DEFINITION) {
				newNode = SemanticSymbol.TERMINATOR.makeNode(currentToken.getLiteral());
				return newNode;
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.UNEXP_TERM + currentToken.getLiteral());
		case TERMINATOR_SCOPE:
			if (parent.getSymbol() == SemanticSymbol.SCOPE_DEFINITION) {
				newNode = SemanticSymbol.TERMINATOR.makeNode(currentToken.getLiteral());
				return newNode;
			}
			throw new InvalidSyntaxException(InvalidSyntaxException.UNEXP_TERM + currentToken.getLiteral());
		default:
			throw new InvalidSyntaxException(InvalidSyntaxException.WTF + currentToken.getLiteral());
		
		}
	}

}
